package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.*;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendServiceImp implements FriendService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override//添加好友
    public void addFriend(Long userId, Long friendId) {
        //创建条件为userId和friendId相同的数据
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));
        Friend myfriend = mongoTemplate.findOne(query, Friend.class);
        //如果不存在 创建好友关系类 赋值并保存
        if (myfriend == null) {
            Friend myFriend = new Friend();
            myFriend.setCreated(System.currentTimeMillis());
            myFriend.setFriendId(friendId);
            myFriend.setUserId(userId);
            mongoTemplate.save(myFriend);
            //查询好友的所有“我的动态”
            List<MyMovement> friendMovements = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friendId);
            //判断非空 并遍历
            if (CollectionUtil.isNotEmpty(friendMovements)) {
                for (MyMovement movement : friendMovements) {
                    //创建“好友动态”类 并赋值
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setUserId(friendId);
                    friendMovement.setCreated(movement.getCreated());
                    friendMovement.setPublishId(movement.getPublishId());
                    //把好友的“我的动态”保存到我的“好友动态”
                    mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + userId);
                }
            }
        }
        //因为好友关系是双向的 所以反向查询关系 看是否存在
        Query query1 = new Query(Criteria.where("friendId").is(userId)
                .and("userId").is(friendId));
        Friend jiarenFriend = mongoTemplate.findOne(query1, Friend.class);
        //如果不存在 创建好友关系类 赋值并保存
        if (jiarenFriend == null) {
            Friend jiaRenFriend = new Friend();
            jiaRenFriend.setUserId(friendId);
            jiaRenFriend.setFriendId(userId);
            jiaRenFriend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(jiaRenFriend);
            //查询出我的所有“我的动态”
            List<MyMovement> myMovements = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);
            //判断非空 并遍历
            if (CollectionUtil.isNotEmpty(myMovements)) {
                for (MyMovement myMovement : myMovements) {
                    //创建好友动态类 并赋值
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setPublishId(myMovement.getPublishId());
                    friendMovement.setCreated(myMovement.getCreated());
                    friendMovement.setUserId(userId);
                    //把我的动态保存到好友的“好友动态”中
                    mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + friendId);
                }
            }
        }
    }

    @Override//查询好友并分页
    public PageBeanVo findFriendByPage(Long userId, Integer pageNum, Integer pageSize) {
        //创建userId相同 按照创建时间降序 并分页
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        //调用mongo查询满足条件的好友关系
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        //调用mongo查询满足条件的数量
        long count = mongoTemplate.count(query, Friend.class);
        //封装分页类 返回
        return new PageBeanVo(pageNum, pageSize, count, friends);
    }

    @Override//删除好友关系
    public void removeFriend(Long userId, Long friendId) {
        //我删好友
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId)),Friend.class);
        //将他的动态信息从你的好友动态表中移除
        mongoTemplate.remove(new Query(Criteria.where("userId").is(friendId)),FriendMovement.class,ConstantUtil.MOVEMENT_FRIEND+userId);
        //好友删我
        mongoTemplate.remove(new Query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId)),Friend.class);
        //将你的动态从他的好友动态表中移除
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)),FriendMovement.class,ConstantUtil.MOVEMENT_FRIEND+friendId);

    }
}
